"""游戏控制面板"""
import pygame
from game_items import *


class HUDPanel(object):
    """所有面板精灵的控制类"""
    margin = 10  # 精灵间距
    white = (255, 255, 255)  # 白色
    gray = (64, 64, 64)  # 灰色

    reward_score = 100000  # 奖励分值
    level2_score = 10000  # 级别2分值
    level3_score = 50000  # 级别3分值

    record_filename = "record.txt"  # 保存最好成绩的文件名

    def __init__(self, display_group):
        # 基本数据
        self.score = 0  # 游戏得分
        self.lives_count = 3  # 生命计数
        self.level = 1  # 游戏等级
        self.best_score = 0  # 最佳成绩
        self.level = 1  # 游戏级别

        # 图像精灵
        self.status_sprite = StatusButton(('pause.png', 'resume.png'), display_group)  # 状态精灵暂停，开始
        self.status_sprite.rect.topleft = (self.margin, self.margin)  # 设定位置

        # 炸弹精灵
        self.bomb_sprite = GameSprite('bomb.png', 0, display_group)  # 炸弹数量
        self.bomb_sprite.rect.x = self.margin  # 设置与左边框距离
        self.bomb_sprite.rect.bottom = SCREEN_RECT.bottom - self.margin  # 设置与底部的距离

        # 得分标签
        self.score_label = Label('score:%d' % self.score, 32, self.gray, display_group)
        self.score_label.rect.midleft = (self.status_sprite.rect.right + self.margin,
                                         self.status_sprite.rect.centery)  # 状态精灵右侧，y与状态矩形相同

        # 游戏级别标签
        self.level_label = Label('level:%d' % self.level, 32, self.gray, display_group)
        self.level_label.rect.midright = (SCREEN_RECT.right - self.margin,
                                          self.status_sprite.rect.centery)

        # 炸弹计数标签
        self.bomb_label = Label('x 3', 32, self.gray, display_group)
        self.bomb_label.rect.midleft = (self.bomb_sprite.rect.right + self.margin,
                                        self.bomb_sprite.rect.centery)

        # 生命计数标签
        self.lives_label = Label('x %d' % self.lives_count, 32, self.gray, display_group)
        self.lives_label.rect.midright = (SCREEN_RECT.right - self.margin,
                                          self.bomb_sprite.rect.centery)

        # 生命精灵
        self.lives_sprite = GameSprite('life.png', 0, display_group)  # 生命值
        self.lives_sprite.rect.right = self.lives_label.rect.x - self.margin  # 设置与右边框的距离
        self.lives_sprite.rect.bottom = SCREEN_RECT.bottom - self.margin  # 设置与底部的距离

        # 游戏暂停或结束时才会显示的标签，先不添加到面板，暂停或结束后再添加
        # 最好成绩标签
        self.best_label = Label('Best:%d' % self.best_score, 36, self.white)
        self.best_label.rect.center = SCREEN_RECT.center

        # 状态标签
        self.status_label = Label('Game Paused!', 48, self.white)
        self.status_label.rect.midbottom = (self.best_label.rect.centerx,
                                            self.best_label.rect.y - 2 * self.margin)

        # 提示标签
        self.tip_label = Label('Press spacebar to continue', 22, self.white)
        self.tip_label.rect.midtop = (self.best_label.rect.centerx,
                                      self.best_label.rect.bottom + 8 * self.margin)

        # 从文件加载最好成绩
        self.load_best_score()

    def show_bomb(self, count):
        """修改炸弹数量为count"""
        self.bomb_label.set_text('x %d' % count)  # 直接调用设置内容函数修改内容，然后需要重新设置位置，原来是1位数，不修正位置两位数可能出界
        self.bomb_label.rect.midleft = (self.bomb_sprite.rect.right + self.margin,
                                        self.bomb_sprite.rect.centery)

    def show_lives(self):
        """修改生命数，生命是根据面板内容改变的，撞毁减少，到达一定分数增加，所以直接使用面板的lives_count计数"""
        self.lives_label.set_text('x %d' % self.lives_count)

        # 修正位置
        self.lives_label.rect.midright = (SCREEN_RECT.right - self.margin,
                                          self.bomb_sprite.rect.centery)
        # 修正生命精灵的位置
        self.lives_sprite.rect.right = self.lives_label.rect.x - self.margin  # 设置与右边框的距离

    def increase_score(self, enemy_score):
        """增加得分，同时处理增加生命，关卡升级，更新最好成绩"""

        # 计算最新得分
        score = self.score + enemy_score

        # 判断是否增加生命
        if score // self.reward_score != self.score // self.reward_score:
            self.lives_count += 1
            self.show_lives()

        self.score = score

        # 更新最好成绩
        self.best_score = score if score > self.best_score else self.best_score

        # 计算关卡等级
        if score < self.level2_score:
            level = 1
        elif score < self.level3_score:
            level = 2
        else:
            level = 3

        is_upgrade = level != self.level
        self.level = level

        # 更新得分精灵显示内容
        self.score_label.set_text("score:%d" % score)
        self.score_label.rect.midleft = (self.status_sprite.rect.right,
                                         self.status_sprite.rect.centery)  # 状态精灵右侧，y与状态矩形相同
        # 更新游戏等级精灵显示内容
        self.level_label.set_text('level:%d' % level)
        self.level_label.rect.midright = (SCREEN_RECT.right - self.margin,
                                          self.status_sprite.rect.centery)

        # 返回是否升级游戏关卡
        return is_upgrade

    def save_best_score(self):
        """保存当前最好得分到文件"""
        file = open(self.record_filename, 'w')
        file.write('%d' % self.best_score)
        file.close()

    def load_best_score(self):
        """从文件读取最高分数"""
        try:
            file = open(self.record_filename, 'r')
            content = file.read()
            file.close()
            # 转换为数字
            self.best_score = int(content)
        except (FileNotFoundError, ValueError):
            print('读取最好成绩文件失败！')

    def panel_paused(self, is_game_over, display_group):
        """游戏暂停或结束需要显示的信息，is_game_over为TRUE则游戏结束"""
        # 判断是否已经显示信息
        if display_group.has(self.best_label, self.status_label, self.tip_label):
            return

        # 根据游戏状态生成提示
        status = 'Game Over!' if is_game_over else 'Game Paused!'
        tip = 'Press spacebar to '
        tip += 'play again.' if is_game_over else 'continue.'

        # 修改标签精灵文本内容
        self.best_label.set_text('Best:%d' % self.best_score)
        self.status_label.set_text(status)
        self.tip_label.set_text(tip)

        # 修正标签精灵位置
        self.best_label.rect.center = SCREEN_RECT.center
        self.status_label.rect.midbottom = (self.best_label.rect.centerx,
                                            self.best_label.rect.y - 2 * self.margin)
        self.tip_label.rect.midtop = (self.best_label.rect.centerx,
                                      self.best_label.rect.bottom + 8 * self.margin)

        # 将精灵放入精灵组
        display_group.add(self.best_label, self.status_label, self.tip_label)

        # 修改状态按钮
        self.status_sprite.switch_status(True)

    def panel_resume(self, display_group):
        """取消停止状态，移除提示信息"""
        display_group.remove(self.best_label, self.status_label, self.tip_label)

        self.status_sprite.switch_status(False)

    def reset_panel(self):
        """重置面板数据"""
        # 数据
        self.score = 0
        self.lives_count = 3

        # 精灵
        self.increase_score(0)
        self.show_bomb(3)
        self.show_lives()
